<?php

if(1){
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");
	header("Content-Transfer-Encoding:utf-8");
	header("Pragma: no-cache");
	header('Content-Type: application/vnd.ms-excel');
	header('Cache-Control: max-age=0');
	header("Content-Disposition:filename=" . "_" . date("Ymd") . time() . ".xls");
}



$titles 	= array("序號", "需求類型", 
					//"姓名", "手機", "住家電話", "公司電話", "住家地址", "大樓名稱", "公司地址", "公司名稱", "Email", "聯絡人", "聯絡人電話", 
					"物件用途", "建物面積1", "建物面積2", "室內面積1", "室內面積2", "土地面積1", "土地面積2", "車位數", "可接受機械", "重點區域1", "重點區域2", "指定大樓1", "指定大樓2", "路段1", "路段2", "物件案名1", "物件案名2", "捷運線1", "捷運站起點1", "捷運站終點1", "捷運線2", "捷運站起點2", "捷運站終點2", "緯度1", "經度1", "緯度2", "經度2", "公園1", "公園2", "學校1", "學校2", "商圈1", "商圈2", "行政區1", "行政區2", "總預算1", "總預算2", "每坪預算1", "每坪預算2", "投報率1", "投報率2", "承租營業用途", "屋齡", "物件案別", "落地窗座向", "該戶座向", "大樓座向", "指定樓層1", "指定樓層2", "指定樓層", "物件型式", "公共設施", "附屬配備", "管理", "房數1", "房數2", "廳數1", "廳數2", "衛數1", "衛數2", "套房數1", "套房數2", "分區使用", "樓地板高度", "客戶來源", "需求等級", "備注", "幣別", "面積單位", "狀態", "無效說明", "接待人", "指派主管", "原經紀人", "經紀人", "建檔時間");
//$titles 	= $selsect_fields;
echo '<table border=1 style="font-size:14px;">';
echo '<tr>';
foreach ($titles as $key => $name) {
	echo '<td>';
	echo $name;
	echo '</td>';
}
echo '</tr>';

$_string='';
$i = 1;


foreach ($result as $key => $val) {

	if($i>=4){

		echo '<tr>';
		echo '<td>' . $i . "</td>";
		
		
		$t_str="";
		if($val->type_rent==1){
				$t_str="租";
		}
		if($val->type_sell==1){
				$t_str.="售";
		}
		if($val->type_consignment>0){
				$t_str.="代銷";
		}

		if($t_str!="")
			echo "<td>".$_string.$t_str."</td>";
		else
			echo "<td></td>";
		/*
		if($val->name)
			echo "<td>".$val->name."</td>";
		else
			echo "<td></td>";

		if($val->mobile!=0)
			echo "<td>".$_string.$val->mobile."</td>";
		else
			echo "<td></td>";

		if($val->home_phone!=0)
			echo "<td>".$_string.$val->home_phone."</td>";
		else
			echo "<td></td>";
		if($val->company_phone!=0)
			echo "<td>".$_string.$val->company_phone."</td>";
		else 
			echo "<td></td>";

		if(!is_numeric($val->home_address))
			echo '<td>'.$val->home_address."</td>";				
		else 
			echo "<td></td>";

		if($val->building_name!=0){
			echo "<td>";
			//echo $_string.$val->building_name;

			foreach ($building_names as  $value ) {
				if($value->building_name_id==$val->building_name )
					echo $value->value;
			}
				//print_r($building_names);

			echo "</td>";
		}else{
			echo "<td></td>";
		}
		if(!is_numeric($val->company_address))
			echo "<td>".$_string.$val->company_address."</td>";
		else
			echo "<td></td>";
		if(!is_numeric($val->company_name)) 
			echo "<td>".$_string.$val->company_name."</td>";
		else
			echo "<td></td>";
		if(!is_numeric($val->email))
			echo "<td>".$_string.$val->email."</td>";
		else
			echo "<td></td>";
		if(!is_numeric($val->contact))
			echo "<td>".$val->contact."</td>";
		else
			echo "<td></td>";	
		if(!is_numeric($val->contact_phone))
			echo "<td>".$_string.$val->contact_phone."</td>";
		else
			echo "<td></td>";
		*/
		//if(isset($object_usage[$val->object_usage]))
		//	echo "<td>".$object_usage[$val->object_usage]."</td>";

		if($val->object_usage!=""){
			echo "<td>";

			//print_r($object_usage);

			foreach($object_usage as $_k =>$_v){
				if($_k==$val->object_usage){
					echo $_v;
				}
				//==$val->object_usage;
			}
			echo "</td>";
		}else{
			echo "<td></td>";
		}
		if($val->building_area1!=0)
			echo "<td>".(int)$val->building_area1."</td>";
		else
			echo "<td></td>";
		if($val->building_area2!=0)
			echo "<td>".(int)$val->building_area2."</td>";
		else
			echo "<td></td>";
		if($val->indoor_area1!=0)
			echo "<td>".(int)$val->indoor_area1."</td>";
		else
			echo "<td></td>";
		if($val->indoor_area2!=0)
			echo "<td>".(int)$val->indoor_area2."</td>";
		else
			echo "<td></td>";
		if($val->land_area1!=0)
			echo "<td>".(int)$val->land_area1."</td>";
		else
			echo "<td></td>";
		if($val->land_area2!=0)
			echo "<td>".(int)$val->land_area2."</td>";
		else
			echo "<td></td>";
		if($val->parking_number!=0)
			echo "<td>".$_string.$val->parking_number."</td>";
		else
			echo "<td></td>";
		if($val->acceptable_mechanical==1) 
			echo "<td>"."可接受機械"."</td>";
		else 
			echo "<td></td>";

		if($val->key_areas1!="")
			echo "<td>".$_string.$val->key_areas1."</td>";
		else
			echo "<td></td>";
		if($val->key_areas2)
			echo "<td>".$_string.$val->key_areas2."</td>";
		else
			echo "<td></td>";
		if($val->specify_building1!="")
			echo "<td>".$_string.$val->specify_building1."</td>";
		else
			echo "<td></td>";
		if($val->specify_building2!="")
			echo "<td>".$_string.$val->specify_building2."</td>";
		else
			echo "<td></td>";
		if($val->sections1!="")
			echo '<td>'.$val->sections1,"</td>";		
		else
			echo "<td></td>";
		if($val->sections2!="")
			echo "<td>".$val->sections2."</td>";
		else
			echo "<td></td>";
		if($val->object_name1!="")
			echo '<td>'.$val->object_name1."</td>";				
		else
			echo "<td></td>";
		if($val->object_name2!="")
			echo "<td>".$val->object_name2."</td>";
		else
			echo "<td></td>";
		if(isset($mrt_line[$val->mrt_line1]))
			echo "<td>".$mrt_line[$val->mrt_line1]."</td>";
		else
			echo "<td></td>";
		if(isset($mrt[$val->mrt_start1]))
			echo "<td>".$mrt[$val->mrt_start1]."</td>";
		else
			echo "<td></td>";
		if(isset($mrt[$val->mrt_until1]))
			echo "<td>".$mrt[$val->mrt_until1]."</td>";
		else
			echo "<td></td>";
		if(isset($mrt_line[$val->mrt_line2]))
			echo "<td>".$mrt_line[$val->mrt_line2]."</td>";
		else
			echo "<td></td>";
		if(isset($mrt[$val->mrt_start2]))
			echo "<td>".$mrt[$val->mrt_start2]."</td>";
		else
			echo "<td></td>";
		if(isset($mrt[$val->mrt_until2]))
			echo "<td>".$mrt[$val->mrt_until2]."</td>";
		else
			echo "<td></td>";
		if($val->range_east!="")
			echo "<td>".$_string.$val->range_east."</td>";
		else
			echo "<td></td>";
		if($val->range_north!="")
			echo "<td>".$_string.$val->range_north."</td>";
		else
			echo "<td></td>";
		if($val->range_west!="")
			echo "<td>".$_string.$val->range_west."</td>";
		else
			echo "<td></td>";
		if($val->range_south!="")
			echo "<td>".$_string.$val->range_south."</td>";
		else
			echo "<td></td>";
		if($val->park1!="")
			echo "<td>".$val->park1."</td>";
		else
			echo "<td></td>";
		if($val->park2!="")
			echo "<td>".$val->park2."</td>";
		else
			echo "<td></td>";
		if($val->school1!="")
			echo "<td>".$val->school1."</td>";
		else
			echo "<td></td>";
		if($val->school2!="")
			echo "<td>".$val->school2."</td>";
		else
			echo "<td></td>";
		if($val->circle1!="")
			echo "<td>".$val->circle1."</td>";
		else
			echo "<td></td>";
		if($val->circle2!="")
			echo "<td>".$val->circle2."</td>";
		else
			echo "<td></td>";
		if($val->administrative_area1!=""){
			echo "<td>".$_string.$val->administrative_area1."</td>";
		}else
			echo "<td></td>";
		if($val->administrative_area2!="")
			echo "<td>".$val->administrative_area2."</td>";				
		else
			echo "<td></td>";
		if($val->total_budget1!=0)
			echo "<td>".(int)$val->total_budget1."</td>";
		else
			echo "<td></td>";
		if($val->total_budget2!=0)
			echo "<td>".(int)$val->total_budget2."</td>";
		else
			echo "<td></td>";
		if($val->budget_per_ping1!=0)
			echo "<td>".$_string.(int)$val->budget_per_ping1."</td>";
		else
			echo "<td></td>";
		if($val->budget_per_ping2!=0)
			echo "<td>".$_string.(int)$val->budget_per_ping2."</td>";
		else
			echo "<td></td>";
		if($val->reported_rate1!=0)
			echo "<td>".$_string.(int)$val->reported_rate1."</td>";
		else
			echo "<td></td>";
		if($val->reported_rate2!=0)
			echo "<td>".$_string.(int)$val->reported_rate2."</td>";
		else
			echo "<td></td>";
		if($val->leasing_business_purposes!="")
			echo "<td>".$_string.$val->leasing_business_purposes."</td>";
		else
			echo "<td></td>";
		if($val->year_of_construction!=0)
			echo "<td>".$_string.$val->year_of_construction."</td>";
		else
			echo "<td></td>";
		
		$oct="";
		if(strstr($val->object_case_type,'1'))
			$oct.="中古屋";
		if(strstr($val->object_case_type,'2')){
			if($oct=="")
				$oct.="預售";
			else
				$oct.="/預售";
		}
		
		if(strstr($val->object_case_type,'3')){
			if($oct=="")
				$oct.="標售";
			else
				$oct.="/標售";
		}
		if($oct!="")
			echo "<td>".$oct."</td>";
		else
			echo "<td></td>";
		if(isset($windows[$val->windows]))
			echo "<td>".$windows[$val->windows]."</td>";
		else
			echo "<td></td>";
		if(isset($base_is_located[$val->facing_the_household]))
			echo "<td>".$base_is_located[$val->facing_the_household]."</td>";
		else
			echo "<td></td>";
		if(isset($base_is_located[$val->building_facing]))
			echo "<td>".$base_is_located[$val->building_facing]."</td>";
		else
			echo "<td></td>";
		if($val->designated_floors1!="")
			echo "<td>".$_string.$val->designated_floors1."</td>";
		else
			echo "<td></td>";
		if($val->designated_floors2!="")
			echo "<td>".$_string.$val->designated_floors2."</td>";
		else
			echo "<td></td>";
		if($val->floors!="")
			echo "<td>".$_string.$val->floors."</td>";
		else
			echo "<td></td>";
		if(isset($object_styles[$val->object_styles]))
			echo "<td>".$object_styles[$val->object_styles]."</td>";
		else
			echo "<td></td>";
		if(isset($public_facilities[$val->public_facilities]))
			echo "<td>".$public_facilities[$val->public_facilities]."</td>";
		else
			echo "<td></td>";
		if(isset($affiliated_with[$val->accessories_with]))
			echo "<td>".$affiliated_with[$val->accessories_with]."</td>";
		else
			echo "<td></td>";
		if(isset($management[$val->management]))
			echo "<td>".$management[$val->management]."</td>";
		else
			echo "<td></td>";
		
		if($val->number_of_rooms1!=0)
			echo "<td>".$_string.$val->number_of_rooms1."</td>";
		else
			echo "<td></td>";
		if($val->number_of_rooms2!=0)
			echo "<td>".$_string.$val->number_of_rooms2."</td>";
		else
			echo "<td></td>";
		if($val->number_of_hall1!=0)
			echo "<td>".$_string.$val->number_of_hall1."</td>";
		else
			echo "<td></td>";
		if($val->number_of_hall2!=0)
			echo "<td>".$_string.$val->number_of_hall2."</td>";
		else
			echo "<td></td>";

		if($val->number_of_toilet1!=0)
			echo "<td>".$_string.$val->number_of_toilet1."</td>";
		else
			echo "<td></td>";
		if($val->number_of_toilet2!=0)
			echo "<td>".$_string.$val->number_of_toilet2."</td>";
		else
			echo "<td></td>";

		if($val->number_of_suite1!=0)
			echo "<td>".$_string.$val->number_of_suite1."</td>";
		else
			echo "<td></td>";
		if($val->number_of_suite2!=0)
			echo "<td>".$_string.$val->number_of_suite2."</td>";
		else
			echo "<td></td>";

		if(isset($land_use_zoning[$val->partition_the]))
			echo "<td>".$land_use_zoning[$val->partition_the]."</td>";
		else
			echo "<td></td>";
		if($val->floor_height!=0)
			echo "<td>".$_string.(int)$val->floor_height."</td>";
		else
			echo "<td></td>";
		if(isset($customer_source[$val->customer_source]))
			echo "<td>".$customer_source[$val->customer_source]."</td>";
		else
			echo "<td></td>";
		if(isset($required_level[$val->required_level]))
			echo "<td>".$required_level[$val->required_level]."</td>";
		else
			echo "<td></td>";
		if($val->remark!="")
			echo "<td>".$_string.$val->remark."</td>";
		else
			echo "<td></td>";
		if(isset($currency[$val->currency]))
			echo "<td>".$currency[$val->currency]."</td>";
		else
			echo "<td></td>";
		if(isset($area_unit[$val->area_unit]))
			echo "<td>".$area_unit[$val->area_unit]."</td>";
		else
			echo "<td></td>";
		if($val->status==1)
			echo "<td>"."有效"."</td>";
		else
			echo "<td>"."無效"."</td>";
		if($val->invalid_description!="")
			echo "<td>".$_string.$val->invalid_description."</td>";
		else
			echo "<td></td>";	

		if($val->front_man!=""){
			echo "<td>";
				foreach ($users as $key => $value) {
					if($val->front_man == $value->users_id){
						echo $value->name;
					}
				}
				//$_string.$val->front_man;
			echo "</td>";
		}else{
			echo "<td></td>";
		}
		if($val->head!=""){
			echo "<td>";
			foreach ($users as $key => $value) {
					if($val->head == $value->users_id){
						echo $value->name;
					}
				}
			
			echo "</td>";
		}else{
			echo "<td></td>";
		}
		
		if($val->original_broker!=""){
			echo "<td>";
				foreach ($users as $key => $value) {
					if($val->original_broker == $value->users_id){
						echo $value->name;
					}
				}
			//echo $_string.$val->original_broker;
			echo "</td>";
		}else{
			echo "<td></td>";
		}
		
		if($val->broker!=""){

			echo "<td>";
			
				foreach ($users as $key => $value) {
					if($val->broker == $value->users_id){
						echo $value->name;
					}
				}
			
			echo "</td>";

		}else{
			echo "<td></td>";
		}
		
		echo "<td>".date("Y/m/d",$val->ctime)."</td>\n";
		
		
		
		
		echo '</tr>';
	}
	$i++;
}

echo '</table>';
?>